<?php
class CustomersController extends AppController {

	var $name = 'Customers';
	var $uses = array('Customer');
	var $view = 'Theme';
    var $theme = HOST;
    
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag')
	);
	
	
	function customer() {

	}
		
	public function beforeFilter(){
		parent::beforeFilter();	
		$this->layout = 'cms';
	}
	
	function index() {
		$this->Customer->recursive = 0;
		$this->set('customers', $this->paginate());
	}


	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid customer', true));
			$this->redirect(array('action' => 'index'));
		}
		// slowniki zdefiniowane w Orders
		App::import('Controller','orders'); 
		$this->set(OrdersController::dictionaries());
		
		$typeOfServices = $this->Customer->Order->TypeOfService->find('list');
		$typesOfSpaces = $this->Customer->Order->TypesOfSpaces->find('list');
		$this->set(compact('typeOfServices', 'typesOfSpaces'));
		
		$this->set('customer', $this->Customer->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {			
			
			$this->data['User']['email'] = $this->data['Customer']['email'];
			$this->data['User']['password'] = $this->Auth->password($this->data['Customer']['password']);	
			$this->data['User']['status'] = 1;
			$this->data['User']['group'] = 'firms';
			if ($this->Customer->validates($this->data)) {
						
				$this->User->create();
				if ($this->User->save($this->data)) {
					$this->data['Customer']['user_id'] = $this->User->getLastInsertID();
					$this->data['Customer']['registration_date'] = date('Y-m-d H:i:s');
					$this->Customer->create();
					if ($this->Customer->save($this->data)) {
						$this->Session->setFlash(__('The customer has been saved', true));
						$this->redirect(array('action' => 'index'));
					} else {
						$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true));
					}
				} else {
					$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true));
				}

			} else {
				$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true), 'flash_error');	
				$this->redirect($this->referer());
			}
		}
		$users = $this->Customer->User->find('list');
		$this->set(compact('users'));
	}
	
	function register() {
		if (!empty($this->data)) {			
			
			$this->data['User']['email'] = $this->data['Customer']['email'];
			$this->data['User']['password'] = $this->Auth->password($this->data['Customer']['password']);	
			$this->data['User']['status'] = 1;
			$this->data['User']['group'] = 'firms';
			if ($this->Customer->validates($this->data)) {
						
				$this->User->create();
				if ($this->User->save($this->data)) {
					$this->data['Customer']['user_id'] = $this->User->getLastInsertID();
					$this->data['Customer']['registration_date'] = date('Y-m-d H:i:s');
					$this->Customer->create();
					if ($this->Customer->save($this->data)) {
						$this->Session->setFlash(__('The customer has been saved', true));						
						$this->redirect($this->referer());
					} else {
						$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true));
					}
				} else {
					$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true));
				}

			} else {
				$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true), 'flash_error');	
				$this->redirect($this->referer());
			}
		}
		$this->layout = 'default';
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid customer', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Customer->save($this->data)) {
				$this->Session->setFlash(__('The customer has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Customer->read(null, $id);
		}
		$users = $this->Customer->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for customer', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Customer->delete($id)) {
			$this->Session->setFlash(__('Customer deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Customer was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

}
?>